/*=========================================================================
| KCalDAV
|--------------------------------------------------------------------------
| (c) 2009  Kumaran Santhanam
|
| This project is released under the GNU General Public License.
| Please see the file COPYING for more details.
|--------------------------------------------------------------------------
| Remote calendar writing class.
 ========================================================================*/

/*=========================================================================
| INCLUDES
 ========================================================================*/

#ifndef KCALDAV_WRITER_H
#define KCALDAV_WRITER_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "job.h"

#include <QtCore/QThread>
#include <QtCore/QString>
#include <QtCore/QTextCodec>
#include <QtCore/QTextDecoder>
#include <QtCore/QDateTime>

namespace KCal {

/*=========================================================================
| CLASS
 ========================================================================*/

/**
 * Calendar writer.
 */
class CalDavWriter : public CalDavJob {
    Q_OBJECT
public:

    /**
     * @param url URL to load.
     */
    CalDavWriter(const QString& url = QString(), const QString& encoding = NULL) :
        CalDavJob(url), mEncoding(encoding)
    {
        clearObjects();
    }

    /**
     * Sets the information about added incidences writer should send to server.
     * @param s icalendar-formatted string consists of all added incidences plus necessary calendar info.
     * May be an empty string, which means there is no added incidences to send.
     */
    void setAddedObjects(const QString& s) {
        mAdded = s;
    }

    /**
     * Sets the information about changed incidences writer should send to server.
     * @param s icalendar-formatted string consists of all changed incidences plus necessary calendar info.
     * May be an empty string, which means there is no changed incidences to send.
     */
    void setChangedObjects(const QString& s) {
        mChanged = s;
    }

    /**
     * Sets the information about deleted incidences writer should send to server.
     * @param s icalendar-formatted string consists of all deleted incidences plus necessary calendar info.
     * May be an empty string, which means there is no deleted incidences to send.
     */
    void setDeletedObjects(const QString& s) {
        mDeleted = s;
    }

    /**
     * Clear all the information previously set.
     */
    void clearObjects() {
        setAddedObjects("");
        setChangedObjects("");
        setDeletedObjects("");
    }

    /**
     * Sets the flag to load all events using the given encoding.
     */
    void setEncoding(const QString& p) {
        mEncoding = p;
    }

protected:

    virtual int runJob(runtime_info* caldavRuntime);

    virtual void cleanJob();

    // Split the data into separate VCALENDAR objects to be operated separately
    // by the caldav library functions.
    template<typename Operation>
    int pushObjects(const QByteArray &data, Operation op, int okCode, runtime_info *RT) {
        if (data.isEmpty() || data[0] == 0)
            return okCode;

        char *urlstr       = strdup(url().toStdString().c_str());
        const char *start  = data.constData();
        const char *end    = 0;

        do {
            char *str = 0;

            end = strstr(start, "\nBEGIN:VCALENDAR");
            if (end) {
                ++end;
                str = strndup(start, end-start);
            }
            else
                str = strdup(start);

            int result = op(str, urlstr, RT);
            free(str);

            if (result != okCode) {
                free(urlstr);
                return result;
            }

            start = end;
        } while (end != 0);

        free(urlstr);
        return okCode;
    }

private:

    QString mEncoding;
    QString mAdded;
    QString mChanged;
    QString mDeleted;
};

} // namespace KCal

#endif // KCALDAV_WRITER_H

